/*
* Copyright (C) 2019 ~ 2021 Uniontech Software Technology Co.,Ltd.
*
* Author:     yanghongwei <yanghongwei@uniontech.com>
*
* Maintainer: yanghongwei <yanghongwei@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef FANOTIFY_H
#define FANOTIFY_H

#include "common.h"

#include <QThread>

#include <string>

class Fanotify : public QThread
{
    Q_OBJECT

public:
    Fanotify(QObject *parent = nullptr);
    ~Fanotify();

    bool isValid();
    // 选择检测对象
    void setNotifyObject(const QString& path);
    // 将fanotify句柄加到select中开始监听
    void startListen();

Q_SIGNALS:
    void operationDir(QString dir);
    void operationHappen(OperationNode node);

protected:
    void run();

private:
    // 获取句柄
    int getNotifyFD() ;
    //当一些程序试图打开文件的时候，fanotify检测到病给内核发送允许访问标志，当然也可以发送不允许访问标志
    int handlePerm(const struct fanotify_event_metadata *metadata);
    //select监测事件
    int selectEvent(fd_set *rfd);
    //获取文件操作事件类型
    int getEvent(const struct fanotify_event_metadata *metadata, int len);
    // 获取文件名跟修改它的进程名称
    QString getFileName(int fd);
    QString getProcessName(int pid);

private:
    QStringList paths ;
    int fanFd ;
};

#endif // FANOTIFY_H
